package org.bees.cms.web.action.io;

import java.io.File;

import org.bees.cms.common.Files;
import org.bees.cms.core.io.Directory;
import org.bees.cms.core.io.Document;
import org.bees.cms.core.io.IO;
import org.bees.cms.service.Service;
import org.bees.cms.web.action.EditActionSupport;
import org.bees.cms.web.util.Pages;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Scope("prototype")
@Controller
public class FileEditAction extends EditActionSupport<IO> {
	private String path;
	private String[] paths;

	private Directory directory;
	private Document document;

	@Override
	public Service<IO> service() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String ui() throws Exception {
		if (paths != null && paths.length > 0) {
			File file = new File(context.getRealPath(paths[0]));
			if (file.isDirectory()) {
				Directory directory = new Directory(paths[0]);
				request.setAttribute("directory", directory);
			} else if (file.isFile()) {
				Document document = new Document(paths[0]);
				request.setAttribute("document", document);
			}
		}
		return UI;
	}

	@Override
	public String create() throws Exception {
		if (directory != null) {
			directory.save();
		}
		if (document != null) {
			document.save();
		}
		request.setAttribute(Pages.MESSAGE, "创建成功!");
		return MESSAGE;
	}

	@Override
	public String modify() throws Exception {
		if (directory != null) {
			if (directory.getPath().trim().equals("")) {
				return create();
			}
			Directory _directory = new Directory(directory.getPath());
			if (!directory.getName().equals(_directory.getName())) {
				_directory.name(directory.getName());
			}
			if (!directory.getParent().getPath().equals(_directory.getParent().getPath())) {
				_directory.move(directory.getParent().getPath());
			}
		}
		if (document != null) {
			if (document.getPath().trim().equals("")) {
				return create();
			}
			Document _document = new Document(document.getPath());
			if (!document.getName().equals(_document.getName())) {
				_document.name(document.getName());
			}
			if (!document.getDirectory().getPath().equals(_document.getDirectory().getPath())) {
				_document.move(document.getDirectory().getPath());
			}
		}
		request.setAttribute(Pages.MESSAGE, "修改成功!");
		return MESSAGE;
	}

	@Override
	public String remove() throws Exception {
		for (int i = 0; paths != null && i < paths.length; i++) {
			File file = new File(Files.absolute(paths[i]));
			if (file.exists() && file.isDirectory()) {
				Directory directory = new Directory(paths[i]);
				directory.omit();
			}
			if (file.exists() && file.isFile()) {
				Document document = new Document(paths[i]);
				document.omit();
			}
		}
		request.setAttribute(Pages.MESSAGE, "删除成功!");
		return MESSAGE;
	}

	public String copy() throws Exception {
		for (int i = 0; paths != null && i < paths.length; i++) {
			File file = new File(Files.absolute(paths[i]));
			if (file.exists() && file.isDirectory()) {
				Directory directory = new Directory(paths[i]);
				directory.copy(path);
			}
			if (file.exists() && file.isFile()) {
				Document document = new Document(paths[i]);
				document.copy(path);
			}
		}
		request.setAttribute(Pages.MESSAGE, "复制成功!");
		return MESSAGE;
	}

	public String hide() throws Exception {
		for (int i = 0; paths != null && i < paths.length; i++) {
			File file = new File(Files.absolute(paths[i]));
			if (file.exists() && file.isDirectory()) {
				Directory directory = new Directory(paths[i]);
				directory.hide();
			}
			if (file.exists() && file.isFile()) {
				Document document = new Document(paths[i]);
				document.hide();
			}
		}
		request.setAttribute(Pages.MESSAGE, "文件隐藏成功!");
		return MESSAGE;
	}

	public String show() throws Exception {
		for (int i = 0; paths != null && i < paths.length; i++) {
			File file = new File(Files.absolute(paths[i]));
			if (file.exists() && file.isDirectory()) {
				Directory directory = new Directory(paths[i]);
				directory.show();
			}
			if (file.exists() && file.isFile()) {
				Document document = new Document(paths[i]);
				document.show();
			}
		}
		request.setAttribute(Pages.MESSAGE, "文件显示成功!");
		return MESSAGE;
	}

	@Override
	public IO getDomain() {
		return domain;
	}

	@Override
	public void setDomain(IO domain) {
		this.domain = domain;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String[] getPaths() {
		return paths;
	}

	public void setPaths(String[] paths) {
		this.paths = paths;
	}

	public Directory getDirectory() {
		return directory;
	}

	public void setDirectory(Directory directory) {
		this.directory = directory;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

}
